package com.fwj.auth.web.user;

import com.alibaba.fastjson.JSONObject;
import com.fwj.auth.annotation.RegisterValidator;
import com.fwj.auth.api.user.UserService;
import com.fwj.auth.entity.user.UserBaseInfo;
import com.fwj.auth.entity.user.UserExtendAccount;
import com.fwj.core.common.annotation.LoginUser;
import com.fwj.core.common.execption.CommonException;
import com.fwj.core.common.model.Mail;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.core.common.model.ResponseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: fwj
 * @Data: 2022/4/19 12:25
 * @Version 1.0
 * @Description:
 */

@RestController
@RequestMapping("/user")
public class AuthController {

    @Autowired
    private UserService userService;

    @PostMapping("/registerIdentifyingCode")
    public ResponseDTO registerIdentifyingCode(@RequestBody JSONObject jsonObject){
        try {
            return userService.registerIdentifyingCode(jsonObject);
        } catch (Exception e) {
            throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    @PostMapping("/register")
    @RegisterValidator
    public ResponseDTO register(@RequestBody JSONObject jsonObject){
        try {
            UserExtendAccount userExtendAccount = JSONObject.parseObject(jsonObject.getJSONObject("userAccount").toJSONString(),UserExtendAccount.class);
            String identityCode = jsonObject.getString("identityCode");
            return userService.saveUserExtendAccount(userExtendAccount,identityCode);
        } catch (Exception e) {
            throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }


    @PostMapping("/logout")
    public ResponseDTO logout(@RequestBody JSONObject jsonObject, HttpServletRequest request){
        try {
            return userService.logout(request);
        } catch (Exception e) {
            throw new CommonException(ResponseEnum.SERVER_ERROR);
        }
    }

    @PostMapping("/saveBindAccount")
    public ResponseDTO saveBindAccount(@RequestBody JSONObject jsonObject, @LoginUser com.fwj.user.entity.user.UserBaseInfo userBaseInfo){
        try {
            UserExtendAccount userExtendAccount = JSONObject.parseObject(jsonObject.toJSONString(),UserExtendAccount.class);
            UserBaseInfo userBaseInfo1 = JSONObject.parseObject(JSONObject.toJSONString(userBaseInfo), UserBaseInfo.class);
            return userService.saveBindAccount(userExtendAccount,userBaseInfo1);
        } catch (Exception e) {
            throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }

    @PostMapping("/getBindAccount")
    public ResponseDTO getBindInfo(@LoginUser com.fwj.user.entity.user.UserBaseInfo userBaseInfo){
        try {
            UserBaseInfo userBaseInfo1 = JSONObject.parseObject(JSONObject.toJSONString(userBaseInfo), UserBaseInfo.class);
            return userService.getBindInfo(userBaseInfo1);
        } catch (Exception e) {
            throw new CommonException(ResponseEnum.DATA_CONVERSION_ERROR);
        }
    }
}
